﻿<ribbon:RibbonWindow xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"  
                     x:Class="centraldba_dbrestore_utility.MainWindow"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
                     xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
                     xmlns:data="clr-namespace:centraldba_dbrestore_utility.Data"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
                     x:Name="RibbonWindow" 
                     Title="Database Restore Utility"
                     HorizontalContentAlignment="Center" 
                     Width="700" 
                     Height="480" 
                     Loaded="RibbonWindow_Loaded" 
                     Closing="RibbonWindow_Closing" 
                     Icon="/centraldba_dbrestore_utility;component/Images/1318623277_database_refresh.ico" StateChanged="RibbonWindow_StateChanged">
    <Window.Resources>
        <!-- ObjectDataProvider: Get a List of SQL Server Instances -->
        <ObjectDataProvider ObjectType="{x:Type data:SQLRecoveryManager_DB}" x:Key="serverList"
                            MethodName="GetSqlServersByCategory" x:Shared="False" IsAsynchronous="True" />
        <CollectionViewSource x:Key="ServerView" Source="{StaticResource serverList}" />

        <!-- Style Triggers -->
        <Style TargetType="ribbon:RibbonGallery" x:Key="DatabasesStyle">
            <Setter Property="DisplayMemberPath" Value="database_name" />
        </Style>
        <Style TargetType="ribbon:RibbonGalleryCategory" x:Key="SqlServersStyle">
            <Setter Property="Header" Value="{Binding Key}" />
            <Setter Property="ItemsSource" Value="{Binding}" />
            <Setter Property="DisplayMemberPath" Value="srv_server_name" />
        </Style>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
        <Style x:Key="Body_Content_DataGrid_Centering" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="Body_Content_DataGrid_RightAlign" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Right" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding file_exists}" Value="False">
                    <Setter Property="Background" Value="Maroon" />
                    <Setter Property="Foreground" Value="LightGray" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ribbon:Ribbon x:Name="Ribbon">
            <ribbon:Ribbon.ContextualTabGroups>
                <ribbon:RibbonContextualTabGroup x:Name="RestoreTabGroup" Header="Restore" Background="CornflowerBlue"  Visibility="Collapsed" />
            </ribbon:Ribbon.ContextualTabGroups>

            <ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonApplicationMenu Label="File">
                    <ribbon:RibbonButton Label="Configure" Click="cmdConfigure_Click" />
                    <ribbon:RibbonButton Label="About" Click="cmdAbout_Click" />
                    <ribbon:RibbonButton Label="Exit" Command="ApplicationCommands.Close" />
                </ribbon:RibbonApplicationMenu>
            </ribbon:Ribbon.ApplicationMenu>

            <ribbon:RibbonTab x:Name="HomeTab" Header="Home">
                <ribbon:RibbonGroup x:Name="grpSource" Header="Select Database">
                    <Grid VerticalAlignment="Center" DataContext="{Binding}">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition x:Name="sourceThirdRow" Height="Auto" />
                        </Grid.RowDefinitions>

                        <StackPanel x:Name="sourceRowServer" Grid.Row="0" Orientation="Horizontal" Margin="3">
                            <ribbon:RibbonComboBox x:Name="lstSourceInstance" 
                                                   Label="Server" 
                                                   KeyTip="D"
                                                   ToolTipTitle="Server"
                                                   ToolTipDescription="This is the server that contains the database whose backup you wish to restore."
                                                   AlternationCount="1"
                                                   SelectionBoxWidth="150" 
                                                   CanUserResizeHorizontally="True"
                                                   CanUserResizeVertically="True"
                                                   IsEditable="True"
                                                   MouseMove="lstInstance_MouseMove">
                                <ribbon:RibbonGallery x:Name="ribbonSourceInstance" 
                                                      MaxColumnCount="1"
                                                      CanUserFilter="True"
                                                      DataContext="{StaticResource ResourceKey=ServerView}"
                                                      ItemsSource="{Binding}"
                                                      ItemContainerStyle="{StaticResource ResourceKey=SqlServersStyle}"
                                                      SelectionChanged="lstSourceInstance_SelectionChanged"
                                                      >
                                    <ribbon:RibbonGallery.FilterItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Key}" />
                                        </DataTemplate>
                                    </ribbon:RibbonGallery.FilterItemTemplate>
                                </ribbon:RibbonGallery>
                            </ribbon:RibbonComboBox>
                        </StackPanel>
                        <StackPanel x:Name="sourceRowDatabase" Grid.Row="1" Orientation="Horizontal" Margin="3">
                            <ribbon:RibbonComboBox x:Name="lstSourceDatabase" 
                                                   Label="Database" 
                                                   ToolTipTitle="Database"
                                                   ToolTipDescription="This is the database whose backup you wish to restore."
                                                   AlternationCount="1"
                                                   SelectionBoxWidth="150"
                                                   CanUserResizeHorizontally="True"
                                                   CanUserResizeVertically="True"
                                                   IsEditable="True"
                                                   MouseMove="lstInstance_MouseMove">
                                <ribbon:RibbonGallery x:Name="ribbonSourceDatabase" 
                                                    MaxColumnCount="1"
                                                    CanUserFilter="False"
                                                    SelectionChanged="lstSourceDatabase_SelectionChanged"
                                                    >
                                    <ribbon:RibbonGalleryCategory ItemsSource="{Binding}"
                                                                DisplayMemberPath="database_name"
                                                                />
                                </ribbon:RibbonGallery>
                            </ribbon:RibbonComboBox>
                        </StackPanel>
                    </Grid>
                </ribbon:RibbonGroup>

                <ribbon:RibbonGroup x:Name="grpFilters" Header="Filters">
                    <Grid VerticalAlignment="Center">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition x:Name="filterThirdRow" Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="170" />
                            <ColumnDefinition Width="80" />
                        </Grid.ColumnDefinitions>

                        <StackPanel x:Name="filterViews" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Orientation="Horizontal">
                            <ribbon:RibbonToggleButton x:Name="togDate" 
                                                       IsChecked="True"
                                                       Checked="togFilter_Checked"
                                                       Unchecked="togFilter_Checked"
                                                       ToolTip="Specific Date"
                                                       Label="Specific Date"
                                                       LargeImageSource="/centraldba_dbrestore_utility;component/Images/base_calendar_32.png" />
                            <ribbon:RibbonToggleButton x:Name="togRecoveryWindow" 
                                                       Checked="togFilter_Checked"
                                                       Unchecked="togFilter_Checked"
                                                       ToolTip="Recovery Window" 
                                                       Label="Recovery Window"
                                                       LargeImageSource="/centraldba_dbrestore_utility;component/Images/Calendar-BusinessMonthView_32.png" />
                            <ribbon:RibbonToggleButton x:Name="togAllFiles" 
                                                       Checked="togFilter_Checked"
                                                       Unchecked="togFilter_Checked"
                                                       Label="All Files"
                                                       ToolTip="All Files"
                                                       LargeImageSource="/centraldba_dbrestore_utility;component/Images/PrintEntireDocument.png" />
                        </StackPanel>

                        <StackPanel x:Name="filterFilesExistPanel" Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Margin="3">
                            <CheckBox x:Name="file_exists" Content="File Exists?" IsChecked="True" Click="file_exists_Click" />
                        </StackPanel>

                        <StackPanel x:Name="filterDatePanel" Grid.Row="0" Grid.Column="1" Margin="3">
                            <DatePicker x:Name="backup_date" SelectedDateChanged="backup_date_SelectedDateChanged" />
                        </StackPanel>

                        <StackPanel x:Name="filterLabelPanel" Grid.Row="1" Grid.Column="1" Margin="3">
                            <ribbon:RibbonComboBox x:Name="lstBackupLabel" 
                                            Margin="3"
                                            SelectionBoxWidth="120" 
                                            CanUserResizeHorizontally="True"
                                            CanUserResizeVertically="True"
                                            IsEditable="True"
                                            MouseMove="lstInstance_MouseMove">
                                <ribbon:RibbonGallery x:Name="ribbonBackupLabel" 
                                                MaxColumnCount="1"
                                                CanUserFilter="False"
                                                SelectionChanged="lstBackupLabel_SelectionChanged">
                                    <ribbon:RibbonGalleryCategory ItemsSource="{Binding}" />
                                </ribbon:RibbonGallery>
                            </ribbon:RibbonComboBox>
                        </StackPanel>
                    </Grid>
                </ribbon:RibbonGroup>
            </ribbon:RibbonTab>

            <ribbon:RibbonTab x:Name="RestoreTab" Header="Restore" ContextualTabGroupHeader="Restore">
                <ribbon:RibbonGroup x:Name="grpDestination" 
                                    Header="Restore to Database" 
                                    ToolTip="Define the database to restore into, if different than the source database">
                    <Grid VerticalAlignment="Center" DataContext="{Binding}">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition x:Name="destThirdRow" Height="Auto" />
                        </Grid.RowDefinitions>

                        <StackPanel x:Name="destRowServer" Grid.Row="0" Orientation="Horizontal" Margin="3">
                            <ribbon:RibbonComboBox x:Name="lstDestinationInstance" 
                                                   Label="Server" 
                                                   AlternationCount="1"
                                                   SelectionBoxWidth="150" 
                                                   CanUserResizeHorizontally="True"
                                                   CanUserResizeVertically="True"
                                                   IsEditable="True"
                                                   MouseMove="lstInstance_MouseMove">
                                <ribbon:RibbonGallery x:Name="ribbonDestinationInstance" 
                                                      MaxColumnCount="1"
                                                      CanUserFilter="True"
                                                      DataContext="{StaticResource ResourceKey=ServerView}"
                                                      ItemsSource="{Binding}"
                                                      ItemContainerStyle="{StaticResource ResourceKey=SqlServersStyle}"
                                                      SelectionChanged="lstDestinationInstance_SelectionChanged"
                                                      >
                                    <ribbon:RibbonGallery.FilterItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Key}" />
                                        </DataTemplate>
                                    </ribbon:RibbonGallery.FilterItemTemplate>
                                </ribbon:RibbonGallery>
                            </ribbon:RibbonComboBox>
                        </StackPanel>
                        <StackPanel x:Name="destRowDatabase" Grid.Row="1" Orientation="Horizontal" Margin="3">
                            <ribbon:RibbonComboBox x:Name="lstDestinationDatabase" 
                                                Label="Database" 
                                                SelectionBoxWidth="150" 
                                                CanUserResizeHorizontally="True"
                                                CanUserResizeVertically="True"
                                                IsEditable="True"
                                                MouseMove="lstInstance_MouseMove">
                                <ribbon:RibbonGallery x:Name="ribbonDestinationDatabase" 
                                                    MaxColumnCount="1"
                                                    CanUserFilter="False"
                                                    SelectionChanged="lstDestinationDatabase_SelectionChanged"
                                                    >
                                    <ribbon:RibbonGalleryCategory ItemsSource="{Binding}"
                                                                DisplayMemberPath="database_name"
                                                                />
                                </ribbon:RibbonGallery>
                            </ribbon:RibbonComboBox>
                        </StackPanel>
                    </Grid>
                    <ribbon:RibbonButton x:Name="cmdCreateDatabase"
                                         CanAddToQuickAccessToolBarDirectly="False" 
                                         IsEnabled="False"
                                         Label="New Database"
                                         ToolTip="Create a new database"
                                         SmallImageSource="Images/1327595509_database_add.png"
                                         LargeImageSource="Images/1327595474_database_add.png"
                                         Click="cmdCreateDatabase_Click"
                                         />
                </ribbon:RibbonGroup>
                <ribbon:RibbonGroup x:Name="grpRestore" Header="Restore Database">
                    <extToolkit:DateTimePicker x:Name="stopat_time" 
                                                   Margin="3"
                                                   Format="Custom" 
                                                   FormatString="MM/dd/yy hh:mm:ss tt" />

                    <ribbon:RibbonButton x:Name="cmdClearDestination"
                                         CanAddToQuickAccessToolBarDirectly="False" 
                                         Label="Clear"
                                         ToolTip="Clear Destination Connection Information"
                                         SmallImageSource="Images/112_ArrowReturnLeft_Blue_16x16_72.png"
                                         LargeImageSource="Images/112_ArrowReturnLeft_Blue_32x32_72.png"
                                         Click="cmdClearDestination_Click"
                                         />
                    <ribbon:RibbonButton x:Name="cmdViewScript"
                                         CanAddToQuickAccessToolBarDirectly="False" 
                                         Label="View"
                                         ToolTip="View Restore Script"
                                         ToolTipDescription="Displays the SQL script that would be used to restore the database"
                                         SmallImageSource="Images/1319557499_old-edit-find.png"
                                         LargeImageSource="Images/1319557497_old-edit-find.png"
                                         Click="cmdViewScript_Click"
                                         />
                    <ribbon:RibbonButton x:Name="cmdRestore"
                                         CanAddToQuickAccessToolBarDirectly="False"
                                         Label="Restore"
                                         ToolTip="Restore Database Now"
                                         ToolTipDescription="Restores the database to the selected backupset"
                                         SmallImageSource="Images/1319557470_database_refresh.png"
                                         LargeImageSource="Images/1319557463_database_refresh.png"
                                         Click="cmdRestore_Click"
                                         />
                </ribbon:RibbonGroup>
            </ribbon:RibbonTab>

        </ribbon:Ribbon>

        <StatusBar Grid.Row="2">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem Grid.Column="1">
                <ProgressBar x:Name="statusProgressBar" Value="30" Maximum="100" Width="80" Height="18" Visibility="Collapsed" />
            </StatusBarItem>
            <StatusBarItem Grid.Column="2">
                <TextBlock x:Name="statusTime" TextAlignment="Right" />
            </StatusBarItem>
            <StatusBarItem>
                <TextBlock x:Name="status" />
            </StatusBarItem>
        </StatusBar>

        <extToolkit:BusyIndicator x:Name="busyDatabaseBackups" 
                                  BusyContent="Loading Backup Data..." 
                                  Grid.Row="1">
            <DataGrid x:Name="dgDatabaseBackups" 
                      ItemsSource="{Binding}" 
                      CellStyle="{StaticResource ResourceKey=Body_Content_DataGrid_Centering}"
                      AutoGenerateColumns="False" 
                      IsReadOnly="True" 
                      SelectionUnit="FullRow"
                      SelectionMode="Single"
                      SelectedCellsChanged="dgDatabaseBackups_SelectedCellsChanged"
                      CanUserReorderColumns="False" 
                      CanUserSortColumns="True"
                      GridLinesVisibility="None">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID" 
                                        Binding="{Binding id}" />
                    <DataGridTextColumn Header="Label" 
                                        Binding="{Binding name}" />
                    <DataGridTextColumn Header="Bkp Type" 
                                        Binding="{Binding type_description}" />
                    <DataGridTextColumn Header="Started"
                                        Binding="{Binding backup_start_date, StringFormat={}{0:MM/dd/yy hh:mm:ss tt}}" />
                    <DataGridTextColumn Header="Size" 
                                        Binding="{Binding backup_size, StringFormat={}{0:N0}}" CellStyle="{StaticResource ResourceKey=Body_Content_DataGrid_RightAlign}" />
                    <DataGridTextColumn Header="Bkp ID" 
                                        Binding="{Binding backup_set_id}" />
                    <DataGridTextColumn Header="Database" 
                                        Binding="{Binding database_name}" />
                    <DataGridCheckBoxColumn Header="Bulk Data?" 
                                            Binding="{Binding has_bulk_logged_data}" />
                    <DataGridCheckBoxColumn Header="Rec Path Broken?" 
                                            Binding="{Binding recovery_path_broken}" />
                    <DataGridCheckBoxColumn Header="File Exists?" 
                                            Binding="{Binding file_exists}" />
                    <DataGridTextColumn Header="File path" 
                                        Binding="{Binding physical_device_name}" />
                </DataGrid.Columns>
            </DataGrid>
        </extToolkit:BusyIndicator>
    </Grid>
</ribbon:RibbonWindow>
